package com.autoglm.ui.components

import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.autoglm.ui.theme.*

/**
 * 虚拟助手组件
 * 3D风格的AI助手头像，支持动画效果
 */
@Composable
fun VirtualAssistant(
    modifier: Modifier = Modifier,
    isListening: Boolean = false,
    isProcessing: Boolean = false,
    onClick: () -> Unit = {}
) {
    // 呼吸动画
    val breathingAnimation by animateFloatAsState(
        targetValue = if (isListening || isProcessing) 1.1f else 1.0f,
        animationSpec = infiniteRepeatable(
            animation = tween(2000, easing = EaseInOut),
            repeatMode = RepeatMode.Reverse
        ),
        label = "breathing"
    )
    
    // 脉冲动画
    val pulseAnimation by animateFloatAsState(
        targetValue = if (isListening) 1.2f else 1.0f,
        animationSpec = infiniteRepeatable(
            animation = tween(1000, easing = EaseInOut),
            repeatMode = RepeatMode.Reverse
        ),
        label = "pulse"
    )
    
    // 旋转动画
    val rotationAnimation by animateFloatAsState(
        targetValue = if (isProcessing) 360f else 0f,
        animationSpec = infiniteRepeatable(
            animation = tween(3000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ),
        label = "rotation"
    )
    
    Box(
        modifier = modifier
            .clip(CircleShape)
            .background(
                brush = Brush.radialGradient(
                    colors = listOf(
                        Accent500.copy(alpha = 0.3f),
                        Accent500.copy(alpha = 0.1f),
                        Color.Transparent
                    )
                )
            )
            .clickable { onClick() },
        contentAlignment = Alignment.Center
    ) {
        // 外层光晕
        if (isListening || isProcessing) {
            Box(
                modifier = Modifier
                    .size(120.dp)
                    .scale(pulseAnimation)
                    .background(
                        brush = Brush.radialGradient(
                            colors = listOf(
                                GlowEffect,
                                Color.Transparent
                            )
                        ),
                        shape = CircleShape
                    )
            )
        }
        
        // 主头像
        Box(
            modifier = Modifier
                .size(100.dp)
                .scale(breathingAnimation)
                .background(
                    brush = Brush.verticalGradient(
                        colors = listOf(
                            GradientCardStart,
                            GradientCardEnd
                        )
                    ),
                    shape = CircleShape
                )
                .border(
                    width = 2.dp,
                    brush = Brush.linearGradient(
                        colors = listOf(
                            Accent500,
                            Accent300
                        )
                    ),
                    shape = CircleShape
                ),
            contentAlignment = Alignment.Center
        ) {
            // 眼睛
            Row(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 左眼
                Box(
                    modifier = Modifier
                        .size(8.dp)
                        .background(
                            color = if (isListening) Accent500 else Accent300,
                            shape = CircleShape
                        )
                )
                
                // 右眼
                Box(
                    modifier = Modifier
                        .size(8.dp)
                        .background(
                            color = if (isListening) Accent500 else Accent300,
                            shape = CircleShape
                        )
                )
            }
            
            // 嘴巴
            Box(
                modifier = Modifier
                    .size(16.dp, 8.dp)
                    .background(
                        color = if (isProcessing) Accent500 else Accent300,
                        shape = CircleShape
                    )
            )
        }
        
        // 状态指示器
        when {
            isListening -> {
                // 声波动画
                repeat(3) { index ->
                    Box(
                        modifier = Modifier
                            .size((20 + index * 10).dp)
                            .background(
                                color = Accent500.copy(alpha = 0.3f - index * 0.1f),
                                shape = CircleShape
                            )
                    )
                }
            }
            isProcessing -> {
                // 加载指示器
                CircularProgressIndicator(
                    modifier = Modifier.size(24.dp),
                    color = Accent500,
                    strokeWidth = 2.dp
                )
            }
        }
    }
}
